package com.googlecode.animator.view.timeline
{
	import flash.display.Sprite;
	import flash.events.ContextMenuEvent;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import mx.containers.Canvas;
	import mx.containers.HDividedBox;
	import mx.containers.VBox;
	import mx.core.ScrollPolicy;
	import mx.core.UIComponent;
	
	public class Timeline_old extends VBox
	{
		private var _mainHBox:HDividedBox;
		private var _controlsHBox:HDividedBox;
		private var _pane:InfoPane;
		private var _gridBox:Canvas;
		private var _timeline:UIComponent;
		private var _grid:Grid;
		private var _thumb:Thumb;
		
		private var _gridNumSpacing:uint;
		private var _vertOffset:int;
		private var _tWidth:uint;
		
		public static const SQUARE_WIDTH:uint = 10;
		
		public function Timeline_old()
		{
			
			horizontalScrollPolicy = ScrollPolicy.OFF;
			verticalScrollPolicy = ScrollPolicy.OFF;
			setStyle("backgroundColor", 0xCCCCCC);
			percentHeight = 100;
			
			
			_mainHBox = new HDividedBox();
			_controlsHBox = new HDividedBox();
			_pane = new InfoPane();
			_gridBox = new Canvas();
			_timeline = new UIComponent();
			_thumb = new Thumb(SQUARE_WIDTH, SQUARE_WIDTH);
			
			_gridNumSpacing = 10;
			_vertOffset = 30;
			_tWidth = 600;
			
			
			//Add a container for all the timeline parts
			makeMainHBox();	
			makeInfoBox();
			makeGridBox();
			makeTimelineContainer(_gridNumSpacing * SQUARE_WIDTH * 10);
			makeTimeline(_gridNumSpacing * SQUARE_WIDTH * 10);
			//makeControlsHBox();
		}
		
		private function makeMainHBox():void
		{
			//mainHBox.height = 120;
			//mainHBox.addEventListener(ResizeEvent.RESIZE, onMainHBoxResize);
			_mainHBox.setStyle("backgroundColor", 0x999999);
			_mainHBox.setStyle("horizontalGap", 3);
			_mainHBox.percentWidth = 100;
			
			_mainHBox.horizontalScrollPolicy = ScrollPolicy.OFF;
			_mainHBox.verticalScrollPolicy = ScrollPolicy.OFF;
			
			addChild(_mainHBox); 
		}
		
		private function makeInfoBox():void
		{
			_pane.vertOffset = _vertOffset;
			_pane.spacing = SQUARE_WIDTH * 2;
			_pane.paneHeight = 130;

			_mainHBox.addChild(_pane);
		}
		
		private function makeGridBox():void
		{
			_gridBox.width = 200;
			//gridBox.height = 150;
			_gridBox.verticalScrollPolicy = ScrollPolicy.OFF;
			//gridBox.addEventListener(MouseEvent.ROLL_OUT, onGridBoxRollOut);

			_mainHBox.addChild(_gridBox);
		}
		
		private function makeTimelineContainer(w:uint):void
		{
			_timeline.width = w;
			_timeline.height = 130;
			
			_gridBox.addChild(_timeline);
		}
		
		private function makeTimeline(w:uint):void
		{
			//Remove strip
			for(var i:uint=0; i<_timeline.numChildren; i++)
			{
				if(_timeline.getChildAt(i).name == "keyFrameStrip")
				{
					_timeline.removeChildAt(i);
					break;
				}		
			}
			if(_grid as Grid == null)
			{
				_grid = new Grid(w, 7, SQUARE_WIDTH, _gridNumSpacing);
				_timeline.addChild(_thumb);
			}
			
			_grid.makeGrid(w, 7, SQUARE_WIDTH, _gridNumSpacing);
			
			_grid.y = _vertOffset;
			_grid.mouseEnabled = false;
			
			//Make a strip that can be right-clicked
			var paste:ContextMenuItem = new ContextMenuItem("Paste keyframe");
			//paste.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onKeyFramePaste);
			
			var customContextMenu:ContextMenu = new ContextMenu();
			customContextMenu.hideBuiltInItems();
			customContextMenu.customItems.push(paste);

			var keyFrameStrip:Sprite = new Sprite();
			keyFrameStrip.name = "keyFrameStrip";
			keyFrameStrip.graphics.beginFill(0x000000, 0);
			keyFrameStrip.graphics.drawRect(0, 0, w * SQUARE_WIDTH, SQUARE_WIDTH);
			keyFrameStrip.contextMenu = customContextMenu;
			
			keyFrameStrip.y = _vertOffset + (SQUARE_WIDTH * 2);
					
			
			_thumb.y = 10;
			
			_timeline.addChild(_grid);
			_timeline.addChild(keyFrameStrip);
			_timeline.setChildIndex(_grid, 0);
			_timeline.setChildIndex(keyFrameStrip, 1);
			_timeline.setChildIndex(_thumb, _timeline.numChildren -1);
		}
		
		private function makeControlsHBox():void
		{
			//transport.addEventListener("stopMarker", onAddGlobalMarker);
			//transport.addEventListener("positionKeyFrame", onAddPositionKeyFrame);
			//transport.addEventListener("hideMarker", onAddHideMarker);
			//transport.addEventListener("extendTimeline", onExtendTimeline);
			
			//controlsHBox.addChild(transport);
			_controlsHBox.width = _tWidth;
			
			addChild(_controlsHBox);
		} 
		protected function onKeyFramePaste(evt:ContextMenuEvent):void
		{	
			/* if(evt.target.label != "Paste keyframe")
			{
				return;
			}

			if(keyFrameCopy as KeyFrame)
			{
				var VO:VO_Object3D = wModel.selectedObject;
				var time:uint = Math.floor(e.mouseTarget.mouseX / SQUARE_WIDTH) * SQUARE_WIDTH;
				cTimeline.insertKeyFrame(time, keyFrameCopy.objPos);
				loadKeyFrames(VO.keyFrames);
				
				//Move thumb and update time
				thumb.x = time;
				cTimeline.selectedTimeInterval = time;
				
				//Finally, rewrite the tweens.
				cTimeline.calculateTweens(VO.keyFrames);
				scrubObjects3D();
			} */
		}
		
	}
}